<?php include("../config.php"); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title><?php echo $sitename; ?> - Sysop: PAD Poller</title>
<meta name="description" content="<?php echo $sitename; ?> - Sysop: PAD Poller">
<meta name="keywords" content="free to try, software, shareware, freeware, developers, vendors, download, PAD">
<meta name="author" content="Andrey Aleksanyants, M.D.">
<meta name="copyright" content="easy HTools">
<meta http-equiv="imagetoolbar" content="no">
<link rel="stylesheet" type="text/css" href="../<?php echo $cssfile; ?>">
<link rel="shortcut icon" href="../favicon.ico">
</head>
<body>
<table class="tmain" align="center" width="90%" cellspacing="0" cellpadding="0">
<tr>
<td class="tmain">
<table class="tlogo" align="center" width="100%" cellspacing="0" cellpadding="4">
<tr>
<td align="right">
<p><a href="index.php">Home</a></p>
</td>
</tr>
</table>
</td>
</tr><tr>
<td class="tmain">
<br>
<table align="center" width="100%" border="0" cellspacing="0" cellpadding="1">
<tr valign="top">
<td class="tbody">
<h1><?php echo $sitename; ?> - Sysop: PAD Poller</h1>
<?php

$maxpoll = 3; // How many consecutive errors can be accepted before removing the PAD

// Connecting to database
if(!($link_id = @mysql_connect($dbhost, $userid, $userpassword))) {
	die("<p><b>Error connecting to the database server:</b> " . mysql_error() . "</p>\n");
}
mysql_select_db($dbname);

$numrows = 0;
$result = mysql_query("SELECT * FROM paddata", $link_id);
if($result) $numrows = mysql_num_rows($result);
echo "<p>There are <b>" . $numrows . "</b> PADs listed in your database.</p>\n";

// Requesting data from Poll PADs form on /sysop/index.php
$pp1       = $_POST["pp1"];
$pp2       = $_POST["pp2"];
$ppbutton  = $_POST["ppbutton"];

if(empty($ppbutton)) {
	echo "<form action=\"" . $PHP_SELF . "\" method=\"post\">\n";
	echo "<table class=\"tvisible\" align=\"center\" width=\"300\" cellspacing=\"0\" cellpadding=\"4\">\n";
	echo "<tr>\n";
	echo "<th class=\"tablehead\" colspan=\"2\">Poll PADs</th>\n";
	echo "</tr><tr>\n";
	echo "<td class=\"tdark\"><p><b>Poll PADs:</b></p></td>\n";
	echo "<td><input type=\"text\" class=\"text\" name=\"pp1\" value=\"1\" size=\"5\"> to <input type=\"text\" class=\"text\" name=\"pp2\" value=\"" . $numrows . "\" size=\"5\"></td>\n";
	echo "</tr><tr>\n";
	echo "<td class=\"tdark\"><p><b>Start:</b></p></td>\n";
	echo "<td><input type=\"submit\" class=\"button\" name=\"ppbutton\" value=\"   Start   \"></td>\n";
	echo "</tr>\n</table>\n</form>\n<br>\n";
}
else { // Processing received data
	if(empty($pp1) or empty($pp2)) {
		echo "<p><b>Error:</b> You did not specify the PADs to poll.<br>Go <a href=\"javascript:history.back()\">Back</a> and fill the form correctly.</p><br>\n";
	}
	else { // Form is OK
		$reqtags = array("<XML_DIZ_INFO><COMPANY_INFO><COMPANY_NAME>",
		                 "<XML_DIZ_INFO><COMPANY_INFO><COUNTRY>",
		                 "<XML_DIZ_INFO><COMPANY_INFO><COMPANY_WEBSITE_URL>",
		                 "<XML_DIZ_INFO><COMPANY_INFO><CONTACT_INFO><CONTACT_FIRST_NAME>",
		                 "<XML_DIZ_INFO><COMPANY_INFO><CONTACT_INFO><CONTACT_LAST_NAME>",
		                 "<XML_DIZ_INFO><COMPANY_INFO><CONTACT_INFO><CONTACT_EMAIL>",
		                 "<XML_DIZ_INFO><PROGRAM_INFO><PROGRAM_NAME>",
		                 "<XML_DIZ_INFO><PROGRAM_INFO><PROGRAM_VERSION>",
		                 "<XML_DIZ_INFO><PROGRAM_INFO><PROGRAM_RELEASE_MONTH>",
		                 "<XML_DIZ_INFO><PROGRAM_INFO><PROGRAM_RELEASE_DAY>",
		                 "<XML_DIZ_INFO><PROGRAM_INFO><PROGRAM_RELEASE_YEAR>",
		                 "<XML_DIZ_INFO><PROGRAM_INFO><PROGRAM_COST_DOLLARS>",
		                 "<XML_DIZ_INFO><PROGRAM_INFO><PROGRAM_TYPE>",
		                 "<XML_DIZ_INFO><PROGRAM_INFO><PROGRAM_INSTALL_SUPPORT>",
		                 "<XML_DIZ_INFO><PROGRAM_INFO><PROGRAM_OS_SUPPORT>",
		                 "<XML_DIZ_INFO><PROGRAM_INFO><PROGRAM_LANGUAGE>",
		                 "<XML_DIZ_INFO><PROGRAM_INFO><PROGRAM_CHANGE_INFO>",
		                 "<XML_DIZ_INFO><PROGRAM_INFO><PROGRAM_CATEGORY_CLASS>",
		                 "<XML_DIZ_INFO><PROGRAM_INFO><PROGRAM_SYSTEM_REQUIREMENTS>",
		                 "<XML_DIZ_INFO><PROGRAM_INFO><FILE_INFO><FILE_SIZE_K>",
		                 "<XML_DIZ_INFO><PROGRAM_DESCRIPTIONS><ENGLISH><KEYWORDS>",
		                 "<XML_DIZ_INFO><PROGRAM_DESCRIPTIONS><ENGLISH><CHAR_DESC_80>",
		                 "<XML_DIZ_INFO><PROGRAM_DESCRIPTIONS><ENGLISH><CHAR_DESC_2000>",
		                 "<XML_DIZ_INFO><WEB_INFO><APPLICATION_URLS><APPLICATION_INFO_URL>",
		                 "<XML_DIZ_INFO><WEB_INFO><APPLICATION_URLS><APPLICATION_SCREENSHOT_URL>",
		                 "<XML_DIZ_INFO><WEB_INFO><APPLICATION_URLS><APPLICATION_ICON_URL>",
		                 "<XML_DIZ_INFO><WEB_INFO><APPLICATION_URLS><APPLICATION_XML_FILE_URL>",
		                 "<XML_DIZ_INFO><WEB_INFO><DOWNLOAD_URLS><PRIMARY_DOWNLOAD_URL>",
		                 "<XML_DIZ_INFO><AFFILIATES><AFFILIATES_ESELLERATE_PRODUCT_ID>",
		                 "<XML_DIZ_INFO><AFFILIATES><AFFILIATES_PLIMUS_PRODUCT_ID>",
		                 "<XML_DIZ_INFO><AFFILIATES><AFFILIATES_REGNOW_PRODUCT_ID>",
		                 "<XML_DIZ_INFO><AFFILIATES><AFFILIATES_SHAREIT_PRODUCT_ID>",
		                 "<XML_DIZ_INFO><ASP><ASP_MEMBER_NUMBER>");
		
		function GetStartTag($parser,$element_name,$element_attribs) {
			global $tagstack;
			$tagstack .= "<" . $element_name . ">";
		}
		
		function GetData($parser,$data) {
			global $tagstack, $tagarray, $lasttag;
			if($lasttag == $tagstack) {
				$tagarray["$tagstack"] .= $data;
			}
			else {
				$tagarray["$tagstack"]  = $data;
			}
			$lasttag = $tagstack;
		}
		
		function GetEndTag($parser,$element_name) {
			global $tagstack, $paderr;
			$x = "<" . $element_name . ">";
			if(strrchr($tagstack,$x) == $x) {
				$tagstack = substr($tagstack,0,strrpos($tagstack,$x));
			}
			else {
				$paderr .= "No corresponding start tag for end tag: " . htmlspecialchars($x) . "\n";
			}
		}
		
		function SetPoll() {
			global $poll, $maxpoll, $removed, $progid, $dbcontact, $dbemail, $dbtitle, $dbpadfile, $paderr, $sitename, $sitemail;
			$poll++;
			if(!mysql_query("UPDATE paddata SET poll = $poll WHERE progid = '$progid'")) {
				die("<p><b>Error updating poll value in paddata:</b> " . mysql_error() . "</p>\n");
			}
			echo "<p>The following polling error occured: " . nl2br(htmlspecialchars($paderr)) . "<br>\n";
			echo "Number of consecutive errors: " . $poll . "</p>\n";
			if($poll<$maxpoll) { // If polling errors number is less then established in maxpoll
				// Sending notification message about polling error
				$subject = $dbtitle . " - PAD polling error on " . $sitename;
				$message = "Dear " . $dbcontact . "\r\n\r\n";
				$message.= "The following error was reported during last PAD polling at the ". $sitename . " downloads site:\r\n\r\n";
				$message.= $paderr . "\r\n\r\n";
				$message.= "PAD url: " . $dbpadfile . "\r\n\r\n";
				$message.= "Once the correction is made, it will be picked up during the next round of PAD polling.\r\n\r\n";
				$message.= "The current number of consecutive polling errors is " . $poll . ". Please note that when number of consecutive PAD polling errors reaches 3, the listing is removed from our database automatically.\r\n\r\n";
				$message.= "Kind regards,\r\n\r\n";
				$message.= $sitename . " download site webmaster\r\n";
				$message.= "http://" . $_SERVER['HTTP_HOST'] . "\r\n";
				$headers = "From: " . $sitename . "<" . $sitemail . ">\r\n";
				$headers.= "Reply-To: " . $sitename . "<" . $sitemail . ">\r\n";
				//$headers.= "Bcc: " . $sitemail . "\r\n"; // to receive the background copies
				$headers.= "Content-Type: text/plain; charset=windows-1252";
			}
			else { // Polling errors number is 3 and higher
				if(!mysql_query("DELETE FROM paddata WHERE progid = '$progid'")) {
					die("<p><b>Error deleting from paddata with poll value higher than 3:</b> " . mysql_error() . "</p>\n");
				}
				$removed++;
				echo "<p><span class=\"red\">PAD \"" . $dbpadfile . "\" (Program ID: " . $progid . ") removed from database.</span></p>\n";
				// Sending notification message about polling error
				$subject = $dbtitle . " has been removed from " . $sitename;
				$message = "Dear " . $dbcontact . "\r\n\r\n";
				$message.= "The following error was reported during last PAD polling at the ". $sitename . " downloads site:\r\n\r\n";
				$message.= $paderr . "\r\n\r\n";
				$message.= "PAD url: " . $dbpadfile . "\r\n\r\n";
				$message.= "Your listing has been automatically removed from our database because there were " . $poll . " consecutive polling errors.\r\n\r\n";
				$message.= "Once the correction is made, you can resubmit your PAD file manually from here:\r\n\r\n";
				$message.= "http://" . $_SERVER['HTTP_HOST'] . "/submit.php\r\n\r\n";
				$message.= "Kind regards,\r\n\r\n";
				$message.= $sitename . " download site webmaster\r\n";
				$headers = "From: " . $sitename . "<" . $sitemail . ">\r\n";
				$headers.= "Reply-To: " . $sitename . "<" . $sitemail . ">\r\n";
				//$headers.= "Bcc: " . $sitemail . "\r\n"; // to receive the background copies
				$headers.= "Content-Type: text/plain; charset=windows-1252";
			}
			if(mail($dbemail, $subject, $message, $headers)) {
				echo "<p>Email successfully sent to " . $dbemail . "</p>\n";
			}
			else {
				echo "<p>Error sending email to " . $dbemail . "</p>\n";
			}
		}
		
		$starttime = time();
		ini_set("max_execution_time","3600");  // 1-hour script timeout
		
		$count    = 0;
		$accepted = 0;
		$updated  = 0;
		$skipped  = 0;
		$removed  = 0;
		
		echo "<h2 class=\"green\">Polling PADs from " . $pp1 . " to " . $pp2 . ":</h2>\n";
		
		$result = mysql_query("SELECT * FROM paddata ORDER BY progid", $link_id);
		
		while($query = mysql_fetch_array($result)) {
			$count++;
			if(($count < $pp1) or($count > $pp2)) {
				$skipped++;
				continue;
			}
			$progid        = $query["progid"];
			$dbcontact     = $query["contactname"];
			$dbemail       = $query["email"];
			$dbtitle       = $query["title"];
			$dbversion     = $query["version"];
			$dbpdate       = $query["pdate"];
			$dbstatus      = $query["status"];
			$dbpadfile     = $query["padfile"];
			$dbcategory    = $query["category"];
			$dbaffiliate   = $query["affiliate"];
			$dbaffiliateid = $query["affiliateid"];
			$poll          = $query["poll"];
			
			echo "<p><b>" . $count . ". PAD URL: " . $dbpadfile . "</b></p>\n";
			
			flush(); // Flush the output PHP buffer
			unset($tagarray); // Unset tag array
			$tagstack = "";
			$lasttag = "?";
			
			$paderr = "";
			
			if(!($data = @file($dbpadfile))) {
				$paderr .= "Error opening PAD URL\n";
				SetPoll();
				continue;
			}
			$data = implode("",$data);
			$data = substr($data, strpos($data,"<?xml"));  // remove any http headers
		
			// Look up the XML encoding
			if(preg_match("/<\?xml [^>]*encoding=['\"](.*?)['\"][^>]*>.*/", $data, $encarray)) {
				$encoding = strtoupper($encarray[1]);
			}
			else {
				$encoding = "UTF-8";
			}
		
			// If the encoding is not natively supported by the PHP XML parser, try to
			// decode on our own (to UTF-8), if decoding is not possible, assume UTF-8,
			// which is the XML default.
			if(($encoding != "UTF-8") &&($encoding != "US-ASCII") &&($encoding != "ISO-8859-1")) {
				$encoding = "";
				if( function_exists("mb_convert_encoding")) {
					$encoded_source = @mb_convert_encoding($data, "UTF-8", $encoding);
					if( $encoded_source != NULL ) {
						$data = str_replace($encarray[0], "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>", $encoded_source);
						$encoding = "UTF-8";
					}
				}
			}
			
			// Create an XML parser
			if(!($xml_parser = xml_parser_create($encoding))) {
				echo "<p><b>Error creating XML parser!</b></p>\n<hr>\n";
				break;
			}
			// Set ISO-8859-1 as target encoding for XML parser
			xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, "ISO-8859-1");
			
			// Set the functions to handle opening and closing tags
			xml_set_element_handler($xml_parser,"GetStartTag","GetEndTag");
			
			// Set the function to handle blocks of character data
			xml_set_character_data_handler($xml_parser,"GetData");
		
			if(!xml_parse($xml_parser,$data,true)) { // Handle errors in parsing
				$paderr .= sprintf("XML parsing error: %s at line %d, column %d\n", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser), xml_get_current_column_number($xml_parser));
			}
			
			// Free up memory used by the XML parser
			xml_parser_free($xml_parser);
		
			if($paderr) {
				SetPoll();
				continue;
			}
			
			// Setting defaults if blank
			$tag = "<XML_DIZ_INFO><COMPANY_INFO><COUNTRY>";
			if(empty($tagarray["$tag"]) or trim($tagarray["$tag"] == "")) $tagarray["$tag"] = "none";
			
			$tag = "<XML_DIZ_INFO><COMPANY_INFO><CONTACT_INFO><CONTACT_FIRST_NAME>";
			if(empty($tagarray["$tag"]) or trim($tagarray["$tag"] == "")) $tagarray["$tag"] = "Anonym";
			
			$tag = "<XML_DIZ_INFO><COMPANY_INFO><CONTACT_INFO><CONTACT_LAST_NAME>";
			if(empty($tagarray["$tag"]) or trim($tagarray["$tag"] == "")) $tagarray["$tag"] = "Author";
			
			$tag = "<XML_DIZ_INFO><PROGRAM_INFO><PROGRAM_COST_DOLLARS>";
			if(empty($tagarray["$tag"]) or trim($tagarray["$tag"] == "")) $tagarray["$tag"] = "0.00";
			
			$tag = "<XML_DIZ_INFO><PROGRAM_INFO><PROGRAM_CHANGE_INFO>";
			if(empty($tagarray["$tag"]) or trim($tagarray["$tag"] == "")) $tagarray["$tag"] = "Not Established";
			
			$tag = "<XML_DIZ_INFO><PROGRAM_INFO><PROGRAM_SYSTEM_REQUIREMENTS>";
			if(empty($tagarray["$tag"]) or trim($tagarray["$tag"] == "")) $tagarray["$tag"] = "Not Established";
			
			$tag = "<XML_DIZ_INFO><PROGRAM_INFO><PROGRAM_LANGUAGE>";
			if(empty($tagarray["$tag"]) or trim($tagarray["$tag"] == "")) $tagarray["$tag"] = "Not Established";
			
			$tag = "<XML_DIZ_INFO><PROGRAM_DESCRIPTIONS><ENGLISH><KEYWORDS>";
			if(empty($tagarray["$tag"]) or trim($tagarray["$tag"] == "")) $tagarray["$tag"] = "free, program, download";
			
			$tag = "<XML_DIZ_INFO><WEB_INFO><APPLICATION_URLS><APPLICATION_SCREENSHOT_URL>";
			if(empty($tagarray["$tag"]) or trim($tagarray["$tag"] == "")) $tagarray["$tag"] = "none";
			
			$tag = "<XML_DIZ_INFO><WEB_INFO><APPLICATION_URLS><APPLICATION_ICON_URL>";
			if(empty($tagarray["$tag"]) or trim($tagarray["$tag"] == "")) $tagarray["$tag"] = "none";
			
			$tag = "<XML_DIZ_INFO><AFFILIATES><AFFILIATES_ESELLERATE_PRODUCT_ID>";
			if(empty($tagarray["$tag"]) or trim($tagarray["$tag"] == "")) $tagarray["$tag"] = "none";

			$tag = "<XML_DIZ_INFO><AFFILIATES><AFFILIATES_PLIMUS_PRODUCT_ID>";
			if(empty($tagarray["$tag"]) or trim($tagarray["$tag"] == "")) $tagarray["$tag"] = "none";

			$tag = "<XML_DIZ_INFO><AFFILIATES><AFFILIATES_REGNOW_PRODUCT_ID>";
			if(empty($tagarray["$tag"]) or trim($tagarray["$tag"] == "")) $tagarray["$tag"] = "none";

			$tag = "<XML_DIZ_INFO><AFFILIATES><AFFILIATES_SHAREIT_PRODUCT_ID>";
			if(empty($tagarray["$tag"]) or trim($tagarray["$tag"] == "")) $tagarray["$tag"] = "none";

			$tag = "<XML_DIZ_INFO><ASP><ASP_MEMBER_NUMBER>";
			if(empty($tagarray["$tag"]) or trim($tagarray["$tag"] == "")) $tagarray["$tag"] = "none";
			
			// Checking for missing/empty tags
			foreach($reqtags as $tag) {
				if(empty($tagarray["$tag"]) or trim($tagarray["$tag"] == "")) {
					$paderr .= "Required tag not found in PAD file: " . $tag . "\n";
				}
			}
			if($paderr) {
				SetPoll();
				continue;
			}
		
			$tag = $reqtags[0];  $company         = substr($tagarray["$tag"], 0, 50);
			$tag = $reqtags[1];  $country         = substr($tagarray["$tag"], 0, 50);
			$tag = $reqtags[2];  $website         = substr($tagarray["$tag"], 0, 128);
			$tag = $reqtags[3];  $firstname       = substr($tagarray["$tag"], 0, 64);
			$tag = $reqtags[4];  $lastname        = substr($tagarray["$tag"], 0, 64);
			$tag = $reqtags[5];  $email           = substr($tagarray["$tag"], 0, 50);
			$tag = $reqtags[6];  $title           = substr($tagarray["$tag"], 0, 50);
			$tag = $reqtags[7];  $version         = substr($tagarray["$tag"], 0, 15);
			$tag = $reqtags[8];  $pmonth          = $tagarray["$tag"];
			$tag = $reqtags[9];  $pday            = $tagarray["$tag"];
			$tag = $reqtags[10]; $pyear           = $tagarray["$tag"];
			$tag = $reqtags[11]; $cost            = $tagarray["$tag"];
			$tag = $reqtags[12]; $ptype           = substr($tagarray["$tag"], 0, 15);
			$tag = $reqtags[13]; $install         = substr($tagarray["$tag"], 0, 50);
			$tag = $reqtags[14]; $os              = substr($tagarray["$tag"], 0, 80);
			$tag = $reqtags[15]; $languages       = substr($tagarray["$tag"], 0, 255);
			$tag = $reqtags[16]; $changeinfo      = $tagarray["$tag"];
			$tag = $reqtags[17]; $category        = substr($tagarray["$tag"], 0, 80);
			$tag = $reqtags[18]; $requirements    = substr($tagarray["$tag"], 0, 200);
			$tag = $reqtags[19]; $ksize           = $tagarray["$tag"];
			$tag = $reqtags[20]; $keywords        = substr($tagarray["$tag"], 0, 128);
			$tag = $reqtags[21]; $description     = substr($tagarray["$tag"], 0, 80);
			$tag = $reqtags[22]; $descrlarge      = substr($tagarray["$tag"], 0, 2000);
			$tag = $reqtags[23]; $homepage        = substr($tagarray["$tag"], 0, 128);
			$tag = $reqtags[24]; $screenshot      = substr($tagarray["$tag"], 0, 128);
			$tag = $reqtags[25]; $icon            = substr($tagarray["$tag"], 0, 128);
			$tag = $reqtags[26]; $padfile         = substr($tagarray["$tag"], 0, 128);
			$tag = $reqtags[27]; $download        = substr($tagarray["$tag"], 0, 128);
			$tag = $reqtags[28]; $esellerateid    = substr($tagarray["$tag"], 0, 16);
			$tag = $reqtags[29]; $plimusid        = substr($tagarray["$tag"], 0, 16);
			$tag = $reqtags[30]; $regnowid        = substr($tagarray["$tag"], 0, 16);
			$tag = $reqtags[31]; $shareitid       = substr($tagarray["$tag"], 0, 16);
			$tag = $reqtags[32]; $aspnumber       = substr($tagarray["$tag"], 0, 5);
			
			// Adding "http://" to links if missing
			if((trim($website) != "") && (strstr($website, "://") === FALSE))
				$website = "http://" . $website;
			if((trim($homepage) != "") && (strstr($homepage, "://") === FALSE))
				$homepage = "http://" . $homepage;
			if((trim($download) != "") && (strstr($download, "://") === FALSE))
				$download = "http://" . $download;
			
			// Establishing contact name
			$contactname = $firstname . " " . $lastname;
			
			// Establishing release date
			if(strlen($pmonth) == 1) $pmonth = "0" . $pmonth;
			if(strlen($pday) == 1) $pday = "0" . $pday;
			$pdate = $pyear . "-" . $pmonth . "-" . $pday;
			// Checking if release date is valid
			$tomorrow = date("Y-m-d",time()+86400);
			if($pdate > $tomorrow) {
				$paderr .= "Error: Program release date (" . $pdate . ") is greater than tomorrow's date (" . $tomorrow . ")\n";
				SetPoll();
				continue;
			}
			// Checking if release dates interval is less then 15 days (1296000 seconds)
			$plastupd = date("Y-m-d",time()-1296000);
			if($dbpdate > $plastupd) {
				$pdate = $dbpdate; // keep old date
			}
			
			// Remove any commas in ksize
			$ksize = str_replace(",","",$ksize);
			
			// Checking Category
			$catfile = "../categories.txt";
			$catary = file($catfile);
			$isvalidcat = false;
			for($i=0; $i<count($catary); $i++) {
				$cdisp = rtrim($catary[$i]);
					if(($category == $cdisp) || ($category == htmlspecialchars($cdisp))) {
					$isvalidcat = true;
					break;
				}
			}
			if(!$isvalidcat) {
				$paderr .= "Error: Unrecognized category " . $category . "\n";
				SetPoll();
				continue;
			}
			
			// Checking for version update and establishing the status
			if($version != $dbversion) {
				$status = 1;
				$updver = 1;
			}
			else {
				$status = $dbstatus;
				$updver = 0;
			}
			
			// Checking screenshot and icon URLs
			if(($screenshot != "none") && (strstr($screenshot, "://") or (strstr($screenshot, ".gif") && strstr($screenshot, ".jpg") && strstr($screenshot, ".png"))) === FALSE) {
				$screenshot = "none";
			}
			if(($icon != "none") && (strstr($icon, "://") or (strstr($icon, ".gif") && strstr($icon, ".jpg") && strstr($icon, ".png"))) === FALSE) {
				$icon = "none";
			}
			
			// Checking PAD URL inside PAD file
			if(strcasecmp($dbpadfile,$padfile) != 0) {
				echo "<p>Changed PAD URL from " . $dbpadfile . " to " . $padfile . "</p>\n";
			}
			
			// Processing affiliate data
			if(!$dbaffiliate || !$dbaffiliateid) { // if no existing affiliate data
				if($esellerateid != "none") {
					$affiliate   = "eSellerate";
					$affiliateid = $esellerateid;
				}
				if($plimusid != "none") {
					$affiliate   = "Plimus";
					$affiliateid = $plimusid;
				}
				if($regnowid != "none") {
					$affiliate   = "RegNow";
					$affiliateid = $regnowid;
				}
				if($shareitid != "none") {
					$affiliate   = "Share-It";
					$affiliateid = $shareitid;
				}
				else {
					$affiliate   = "";
					$affiliateid = "";
				}
			}
			else { // keep old data
				$affiliate = $dbaffiliate;
				$affiliateid = $dbaffiliateid;
			}
			
			$company       = addslashes($company);
			$country       = addslashes($country);
			$contactname   = addslashes($contactname);
			$title         = addslashes($title);
			$version       = addslashes($version);
			$changeinfo    = addslashes($changeinfo);
			$requirements  = addslashes($requirements);
			$keywords      = addslashes($keywords);
			$description   = addslashes($description);
			$descrlarge    = addslashes($descrlarge);
			$poll          = 0;
			
			// Updating database
			if(!mysql_query("UPDATE paddata SET company = '$company', country = '$country', website = '$website', contactname = '$contactname', email = '$email', title = '$title', version = '$version', pdate = '$pdate', cost = '$cost', ptype = '$ptype', status = '$status', install = '$install', os = '$os', languages = '$languages', changeinfo = '$changeinfo', category = '$category', requirements = '$requirements', ksize = '$ksize', keywords = '$keywords', description = '$description', descrlarge = '$descrlarge', homepage = '$homepage', screenshot = '$screenshot', icon = '$icon', padfile = '$padfile', download = '$download', aspnumber = '$aspnumber', affiliate = '$affiliate', affiliateid = '$affiliateid', poll = '$poll' WHERE progid = '$progid'", $link_id)) {
				echo "<p><b>Error updating existing data in paddata table:</b> " . mysql_error() . "</p>\n";
				continue;
			}
			if($updver == 1) {
				$updated++;
				echo "<p>\"" . $title . "\" version has been updated from " . $dbversion . " to " . $version . ".</p>\n";
				
				$subject  = $title . " has been updated on " . $sitename;
				$message  = "Dear " . $contactname . "!\r\n\r\n";
				$message .= "Your software \"" . $title . "\" has been updated on " . $sitename . " according to version changes in PAD file.\r\n";
				$message .= "Old version: " . $dbversion . "\r\n";
				$message .= "New version: " . $version . "\r\n\r\n";
				$message .= "You can view your software listing here: http://" . $_SERVER['HTTP_HOST'] . "/details.php?progid=" . $progid . "\r\n\r\n";
				$message .= "If your software has been rated at " . $sitename . ", you can get the award logo from http://" . $_SERVER['HTTP_HOST'] . "/awards.php and link it to your software description page.\r\n";
				$message .= "If your software has not been rated yet, you can apply for " . $sitename . " award from http://" . $_SERVER['HTTP_HOST'] . "/applyaward.php\r\n\r\n";
				//$message .= "If you are interested in your software promotion on " . $sitename . " and all the other sites of easy HTools Network then please refer to http://" . $_SERVER['HTTP_HOST'] . "/promote.php\r\n\r\n";
				$message .= "Kind regards,\r\n\r\n";
				$message .= $sitename . " webmaster\r\n";
				$headers  = "From: " . $sitemail . "<" . $sitemail . ">\r\n";
				$headers .= "Reply-To: " . $sitemail . "<" . $sitemail . ">\r\n";
				$headers .= "Content-Type: text/plain; charset=windows-1252";
				if(mail($email, $subject, $message, $headers)) {
					echo "<p>Email successfully sent to " . $email . "</p>\n";
				}
				else {
					echo "<p>Error sending email to " . $email . "</p>\n";
				}
			}
			$accepted++;
		}
		
		echo "<p><b>PAD files processed:</b> " . $count . "</p>\n";
		echo "<p><b>PAD files skipped:</b> " . $skipped . "</p>\n";
		echo "<p><b>PAD files accepted:</b> " . $accepted . "</p>\n";
		echo "<p><b>PAD files rejected:</b> " . ($count-$accepted-$skipped) . "<p>\n";
		echo "<p><b>PAD files removed:</b> " . $removed . "<p><br>\n";
		
		echo "<p><b>Program versions updated:</b> " . $updated . "</p><br>\n";
		
		$elapsed = time() - $starttime;
		$min = intval($elapsed / 60);
		$sec = $elapsed % 60;
		echo "<p><b>Elapsed time:</b> " . $min . " minutes, " . $sec . " seconds.</p><br>\n";
	}
}
// Disconnect from database
mysql_close($link_id);
?>
<br>

</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>